//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FwdToCotSwapAdapterFactory.h"
using namespace Cephei::QL::Models::Marketmodels::Models;
#include <gen/QL/Models/Marketmodels/MarketModel.h>
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/MarketModelFactory.h>
using namespace Cephei::QL::Models::Marketmodels;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::CFwdToCotSwapAdapterFactory (Cephei::QL::Models::Marketmodels::IMarketModelFactory^ forwardFactory) : CMarketModelFactory(CFwdToCotSwapAdapterFactory::typeid)
{
    CMarketModelFactory^ _CforwardFactory;
    try
    {
#ifdef HANDLE
        _phFwdToCotSwapAdapterFactory = NULL;
#endif
        _CforwardFactory = safe_cast<CMarketModelFactory^> (forwardFactory);
        _CforwardFactory->Lock();
        boost::shared_ptr<QuantLib::MarketModelFactory>& _forwardFactory = static_cast<boost::shared_ptr<QuantLib::MarketModelFactory>&> (_CforwardFactory->GetShared ()); 
        _ppFwdToCotSwapAdapterFactory = new boost::shared_ptr<QuantLib::FwdToCotSwapAdapterFactory> (new QuantLib::FwdToCotSwapAdapterFactory ( _forwardFactory ));
        SetMarketModelFactory (boost::dynamic_pointer_cast<QuantLib::MarketModelFactory> (*_ppFwdToCotSwapAdapterFactory));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CforwardFactory != nullptr) _CforwardFactory->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::CFwdToCotSwapAdapterFactory (boost::shared_ptr<QuantLib::FwdToCotSwapAdapterFactory>& childNative, Object^ owner) : CMarketModelFactory(CFwdToCotSwapAdapterFactory::typeid)
{
#ifdef HANDLE
	_phFwdToCotSwapAdapterFactory = NULL;
#endif
	_ppFwdToCotSwapAdapterFactory = &childNative;
    _ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (boost::dynamic_pointer_cast<QuantLib::MarketModelFactory> (*_ppFwdToCotSwapAdapterFactory));
}
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::CFwdToCotSwapAdapterFactory (QuantLib::FwdToCotSwapAdapterFactory& childNative, Object^ owner) : CMarketModelFactory(CFwdToCotSwapAdapterFactory::typeid)
{
#ifdef HANDLE
	_phFwdToCotSwapAdapterFactory = NULL;
#endif
	_ppFwdToCotSwapAdapterFactory = new boost::shared_ptr<QuantLib::FwdToCotSwapAdapterFactory> (&childNative);
    _ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (boost::dynamic_pointer_cast<QuantLib::MarketModelFactory> (*_ppFwdToCotSwapAdapterFactory));
    _FwdToCotSwapAdapterFactoryOwner = owner;
    _MarketModelFactoryOwner = owner;
}

Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::CFwdToCotSwapAdapterFactory (CFwdToCotSwapAdapterFactory^ copy) : CMarketModelFactory(CFwdToCotSwapAdapterFactory::typeid)
{
#ifdef HANDLE
	_phFwdToCotSwapAdapterFactory = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFwdToCotSwapAdapterFactory = new boost::shared_ptr<QuantLib::FwdToCotSwapAdapterFactory> (copy->GetShared());
        _ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (boost::dynamic_pointer_cast<QuantLib::MarketModelFactory> (*_ppFwdToCotSwapAdapterFactory));
    }
}
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::CFwdToCotSwapAdapterFactory (System::Type^ t) : CMarketModelFactory(CFwdToCotSwapAdapterFactory::typeid)
{
#ifdef HANDLE
	_phFwdToCotSwapAdapterFactory = NULL;
#endif
	if (!t->IsSubclassOf(CFwdToCotSwapAdapterFactory::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::CFwdToCotSwapAdapterFactory (QuantLib::Handle<QuantLib::FwdToCotSwapAdapterFactory>& childNative, Object^ owner)  : CMarketModelFactory(CFwdToCotSwapAdapterFactory::typeid)
{
	_phFwdToCotSwapAdapterFactory = &childNative;
	_ppFwdToCotSwapAdapterFactory = &static_cast<boost::shared_ptr<QuantLib::FwdToCotSwapAdapterFactory>>(childNative.currentLink());
    _ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (boost::dynamic_pointer_cast<QuantLib::MarketModelFactory> (*_ppFwdToCotSwapAdapterFactory));
    _FwdToCotSwapAdapterFactoryOwner = owner;
}
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::CFwdToCotSwapAdapterFactory (QuantLib::Handle<QuantLib::FwdToCotSwapAdapterFactory> childNative)  : CMarketModelFactory(CFwdToCotSwapAdapterFactory::typeid)
{
	_phFwdToCotSwapAdapterFactory = &childNative;
	_ppFwdToCotSwapAdapterFactory = &static_cast<boost::shared_ptr<QuantLib::FwdToCotSwapAdapterFactory>>(childNative.currentLink());
    _ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (boost::dynamic_pointer_cast<QuantLib::MarketModelFactory> (*_ppFwdToCotSwapAdapterFactory));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::CFwdToCotSwapAdapterFactory (QuantLib::FwdToCotSwapAdapterFactory childNative)  : CMarketModelFactory(CFwdToCotSwapAdapterFactory::typeid)
{
#ifdef HANDLE
	_phFwdToCotSwapAdapterFactory = NULL;
#endif
	_ppFwdToCotSwapAdapterFactory = new boost::shared_ptr<QuantLib::FwdToCotSwapAdapterFactory> (new QuantLib::FwdToCotSwapAdapterFactory (childNative));
    _ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (boost::dynamic_pointer_cast<QuantLib::MarketModelFactory> (*_ppFwdToCotSwapAdapterFactory));
}
#endif

Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::~CFwdToCotSwapAdapterFactory ()
{
    if (_ppFwdToCotSwapAdapterFactory != NULL)
    {
	    delete _ppFwdToCotSwapAdapterFactory;
        _ppFwdToCotSwapAdapterFactory = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::!CFwdToCotSwapAdapterFactory ()
{
    if (_ppFwdToCotSwapAdapterFactory != NULL)
    {
	    delete _ppFwdToCotSwapAdapterFactory;
    }
}
QuantLib::FwdToCotSwapAdapterFactory& Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::GetReference ()
{
    if (_ppFwdToCotSwapAdapterFactory == NULL) throw gcnew NativeNullException ();
	return **_ppFwdToCotSwapAdapterFactory;
}
boost::shared_ptr<QuantLib::FwdToCotSwapAdapterFactory>& Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::GetShared ()
{
    if (_ppFwdToCotSwapAdapterFactory == NULL) throw gcnew NativeNullException ();
	return *_ppFwdToCotSwapAdapterFactory;
}
QuantLib::FwdToCotSwapAdapterFactory* Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::GetPointer ()
{
    if (_ppFwdToCotSwapAdapterFactory == NULL) throw gcnew NativeNullException ();
	return &**_ppFwdToCotSwapAdapterFactory;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FwdToCotSwapAdapterFactory>& Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::GetHandle ()
{
	if (_phFwdToCotSwapAdapterFactory == NULL)
	{
		_phFwdToCotSwapAdapterFactory = new Handle<QuantLib::FwdToCotSwapAdapterFactory> (*_ppFwdToCotSwapAdapterFactory);
	}
	return *_phFwdToCotSwapAdapterFactory;
}
#endif
bool Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::HasNative () 
{
	return (_ppFwdToCotSwapAdapterFactory != NULL);
}

Cephei::QL::Models::Marketmodels::IMarketModel^ Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::Create (Cephei::QL::Models::Marketmodels::IEvolutionDescription^ evolution, UInt64 numberOfFactors)
{
    CEvolutionDescription^ _Cevolution;
    try
    {
        _Cevolution = safe_cast<CEvolutionDescription^> (evolution);
        _Cevolution->Lock();
        QuantLib::EvolutionDescription& _evolution = static_cast<QuantLib::EvolutionDescription&> (_Cevolution->GetReference ()); 
        QuantLib::Size _numberOfFactors = (QuantLib::Size)ValueHelper::Convert (numberOfFactors);
    	boost::shared_ptr<QuantLib::MarketModel> _rv = (boost::shared_ptr<QuantLib::MarketModel>)(*_ppFwdToCotSwapAdapterFactory)->create ( _evolution,  _numberOfFactors );   
        Cephei::QL::Models::Marketmodels::CMarketModel^ _nrv = gcnew Cephei::QL::Models::Marketmodels::CMarketModel (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cevolution != nullptr) _Cevolution->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Models::IFwdToCotSwapAdapterFactory^ Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppFwdToCotSwapAdapterFactory)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Models::IFwdToCotSwapAdapterFactory^ Cephei::QL::Models::Marketmodels::Models::CFwdToCotSwapAdapterFactory_Factory::Create (Cephei::QL::Models::Marketmodels::IMarketModelFactory^ forwardFactory)
{
    return gcnew CFwdToCotSwapAdapterFactory ( forwardFactory);
}
